package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.bwie.pojo.Car;
import com.bwie.service.CarService;
import com.bwie.utils.R;
import com.bwie.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @Auther:${涛}
 * @Date:2024/8/8 08:46
 * @Description: CarController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/car")
public class CarController {


    @Autowired
    private CarService carService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    KafkaTemplate kafkaTemplate;

    @PostMapping("/list")
    public R list() {
        List<Car> list = carService.list();

        MessageVo messageVo = new MessageVo();
        String msgId = UUID.randomUUID().toString();
        String msgBody = JSON.toJSONString(list);
        messageVo.setMsgBody(msgBody);
        messageVo.setMsgId(msgId);

        String msg = JSON.toJSONString(messageVo);
        stringRedisTemplate.opsForValue().set(msgId, msgBody);
        kafkaTemplate.send("aaa",msg);
        return R.OK(list);
    }
}


